home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVMEM2.C < prev    next >
C/C++ Source or Header  |  1992-03-05  |  15KB  |  502 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevmem2.c */
  21. /* Polybit "memory" (stored bitmap) devices for Ghostscript library. */
  22. #include "memory_.h"
  23. #include "gs.h"
  24. #include "gxdevice.h"
  25. #include "gxdevmem.h"            /* semi-public definitions */
  26. #include "gdevmem.h"            /* private definitions */
  27.  
  28. /* ------ Generic procedures ------ */
  29.  
  30. /* Copy a rectangle of bytes from a source to a destination. */
  31. #undef chunk
  32. #define chunk byte
  33. private int
  34. copy_byte_rect(gx_device_memory *dev, const byte *source, int sraster,
  35.   int offset, int y, int byte_count, int h)
  36. {    uint draster = dev->raster;
  37.     byte *dest = scan_line_base(dev, y) + offset;
  38.     while ( h-- > 0 )
  39.        {    memcpy(dest, source, byte_count);
  40.         source += sraster;
  41.         dest += draster;
  42.        }
  43.     return 0;
  44. }
  45.  
  46. /* ------ Mapped 8-bit color ------ */
  47.  
  48. /* Procedures */
  49. declare_mem_map_procs(mem_mapped_map_rgb_color, mem_mapped_map_color_rgb);
  50. declare_mem_procs(mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle);
  51.  
  52. /* The device descriptor. */
  53. private gx_device_procs mem_mapped8_procs =
  54.   mem_procs(mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  55.     mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle);
  56.  
  57. /* The instance is public. */
  58. gx_device_memory mem_mapped8_color_device =
  59.   mem_device("image(8)", 8, mem_mapped8_procs);
  60.  
  61. /* Convert x coordinate to byte offset in scan line. */
  62. #undef x_to_byte
  63. #define x_to_byte(x) (x)
  64.  
  65. /* Map a r-g-b color to a color index. */
  66. /* This requires searching the palette. */
  67. private gx_color_index
  68. mem_mapped_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  69.   gx_color_value b)
  70. {    byte br = gx_color_value_to_byte(r);
  71.     byte bg = gx_color_value_to_byte(g);
  72.     byte bb = gx_color_value_to_byte(b);
  73.     register byte *pptr = mdev->palette;
  74.     int cnt = mdev->palette_size;
  75.     byte *which;
  76.     int best = 256*3;
  77.     while ( cnt-- > 0 )
  78.        {    register int diff = *pptr - br;
  79.         if ( diff < 0 ) diff = -diff;
  80.         if ( diff < best )    /* quick rejection */
  81.            {    int dg = pptr[1] - bg;
  82.             if ( dg < 0 ) dg = -dg;
  83.             if ( (diff += dg) < best )    /* quick rejection */
  84.                {    int db = pptr[2] - bb;
  85.                 if ( db < 0 ) db = -db;
  86.                 if ( (diff += db) < best )
  87.                     which = pptr, best = diff;
  88.                }
  89.            }
  90.         pptr += 3;
  91.        }
  92.     return (gx_color_index)((which - mdev->palette) / 3);
  93. }
  94.  
  95. /* Map a color index to a r-g-b color. */
  96. private int
  97. mem_mapped_map_color_rgb(gx_device *dev, gx_color_index color,
  98.   gx_color_value prgb[3])
  99. {    byte *pptr = mdev->palette + (int)color * 3;
  100.     prgb[0] = gx_color_value_from_byte(pptr[0]);
  101.     prgb[1] = gx_color_value_from_byte(pptr[1]);
  102.     prgb[2] = gx_color_value_from_byte(pptr[2]);
  103.     return 0;
  104. }
  105.  
  106. /* Fill a rectangle with a color. */
  107. private int
  108. mem_mapped8_fill_rectangle(gx_device *dev,
  109.   int x, int y, int w, int h, gx_color_index color)
  110. {    declare_scan_ptr(dest);
  111.     check_rect();
  112.     setup_rect(dest);
  113.     while ( h-- > 0 )
  114.        {    memset(dest, (byte)color, w);
  115.         inc_chunk_ptr(dest, draster);
  116.        }
  117.     return 0;
  118. }
  119.  
  120. /* Copy a monochrome bitmap. */
  121. private int
  122. mem_mapped8_copy_mono(gx_device *dev,
  123.   byte *base, int sourcex, int sraster, gx_bitmap_id id,
  124.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  125. {    byte *line;
  126.     int first_bit;
  127.     declare_scan_ptr(dest);
  128.     check_rect();
  129.     setup_rect(dest);
  130.     line = base + (sourcex >> 3);
  131.     first_bit = 0x80 >> (sourcex & 7);
  132.     while ( h-- > 0 )
  133.        {    register byte *pptr = dest;
  134.         byte *sptr = line;
  135.         register int sbyte = *sptr;
  136.         register uint bit = first_bit;
  137.         int count = w;
  138. #define is_color(c) ((int)(c) != (int)gx_no_color_index)
  139. #define next_bit()\
  140.   if ( (bit >>= 1) == 0 ) bit = 0x80, sbyte = *++sptr;\
  141.   pptr++
  142.         if ( is_color(one) )
  143.            {    if ( is_color(zero) )
  144.                {    /* Optimize halftone coloring */
  145.                 do
  146.                    {    *pptr = (sbyte & bit ? (byte)one :
  147.                          (byte)zero);
  148.                     next_bit();
  149.                    }
  150.                 while ( --count > 0 );
  151.                }
  152.             else
  153.                {    /* Optimize stenciling */
  154.                 do
  155.                    {    if ( sbyte & bit )
  156.                       *pptr = (byte)one;
  157.                     next_bit();
  158.                    }
  159.                 while ( --count > 0 );
  160.                }
  161.            }
  162.         else if ( is_color(zero) )
  163.            {    do
  164.                {    if ( !(sbyte & bit) )
  165.                   *pptr = (byte)zero;
  166.                 next_bit();
  167.                }
  168.             while ( --count > 0 );
  169.            }
  170. #undef next_bit
  171. #undef is_color
  172.         line += sraster;
  173.         inc_chunk_ptr(dest, draster);
  174.        }
  175.     return 0;
  176. }
  177.  
  178. /* Copy a color bitmap. */
  179. private int
  180. mem_mapped8_copy_color(gx_device *dev,
  181.   byte *base, int sourcex, int sraster, gx_bitmap_id id,
  182.   int x, int y, int w, int h)
  183. {    check_rect();
  184.     return copy_byte_rect(mdev, base + x_to_byte(sourcex), sraster,
  185.         x_to_byte(x), y, x_to_byte(w), h);
  186. }
  187.  
  188. /* ------ 16-bit true color ------ */
  189. /* The 16 bits are divided 5 for red, 6 for green, and 5 for blue. */
  190.  
  191. /* Procedures */
  192. declare_mem_map_procs(mem_true16_map_rgb_color, mem_true16_map_color_rgb);
  193. declare_mem_procs(mem_true16_copy_mono, mem_true16_copy_color, mem_true16_fill_rectangle);
  194.  
  195. /* The device descriptor. */
  196. private gx_device_procs mem_true16_procs =
  197.   mem_procs(mem_true16_map_rgb_color, mem_true16_map_color_rgb,
  198.     mem_true16_copy_mono, mem_true16_copy_color, mem_true16_fill_rectangle);
  199.  
  200. /* The instance is public. */
  201. gx_device_memory mem_true16_color_device =
  202.   mem_device("image(16)", 16, mem_true16_procs);
  203.  
  204. /* Map a r-g-b color to a color index. */
  205. private gx_color_index
  206. mem_true16_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  207.   gx_color_value b)
  208. {    return ((r >> (gx_color_value_bits - 5)) << 11) +
  209.         ((g >> (gx_color_value_bits - 6)) << 5) +
  210.         (b >> (gx_color_value_bits - 5));
  211. }
  212.  
  213. /* Map a color index to a r-g-b color. */
  214. private int
  215. mem_true16_map_color_rgb(gx_device *dev, gx_color_index color,
  216.   gx_color_value prgb[3])
  217. {    ushort value;
  218.     value = color >> 11;
  219.     prgb[0] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  220.     value = (color >> 6) & 0x7f;
  221.     prgb[1] = ((value << 10) + (value << 4) + (value >> 2)) >> (16 - gx_color_value_bits);
  222.     value = color & 0x3f;
  223.     prgb[2] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  224.     return 0;
  225. }
  226.  
  227. /* Convert x coordinate to byte offset in scan line. */
  228. #undef x_to_byte
  229. #define x_to_byte(x) ((x) << 1)
  230.  
  231. /* Fill a rectangle with a color. */
  232. private int
  233. mem_true16_fill_rectangle(gx_device *dev,
  234.   int x, int y, int w, int h, gx_color_index color)
  235. {    declare_scan_ptr(dest);
  236.     check_rect();
  237.     setup_rect(dest);
  238.     while ( h-- > 0 )
  239.        {    ushort *pptr = (ushort *)dest;
  240.         int cnt = w;
  241.         do { *pptr++ = (ushort)color; } while ( --cnt > 0 );
  242.         inc_chunk_ptr(dest, draster);
  243.        }
  244.     return 0;
  245. }
  246.  
  247. /* Copy a monochrome bitmap. */
  248. private int
  249. mem_true16_copy_mono(gx_device *dev,
  250.   byte *base, int sourcex, int sraster, gx_bitmap_id id,
  251.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  252. {    byte *line;
  253.     int first_bit;
  254.     declare_scan_ptr(dest);
  255.     check_rect();
  256.     setup_rect(dest);
  257.     line = base + (sourcex >> 3);
  258.     first_bit = 0x80 >> (sourcex & 7);
  259.     while ( h-- > 0 )
  260.        {    register ushort *pptr = (ushort *)dest;
  261.         byte *sptr = line;
  262.         register int sbyte = *sptr++;
  263.         register int bit = first_bit;
  264.         int count = w;
  265.         do
  266.            {    if ( sbyte & bit )
  267.                {    if ( one != gx_no_color_index )
  268.                   *pptr = (ushort)one;
  269.                }
  270.             else
  271.                {    if ( zero != gx_no_color_index )
  272.                   *pptr = (ushort)zero;
  273.                }
  274.             if ( (bit >>= 1) == 0 )
  275.                 bit = 0x80, sbyte = *sptr++;
  276.             pptr++;
  277.            }
  278.         while ( --count > 0 );
  279.         line += sraster;
  280.         inc_chunk_ptr(dest, draster);
  281.        }
  282.     return 0;
  283. }
  284.  
  285. /* Copy a color bitmap. */
  286. private int
  287. mem_true16_copy_color(gx_device *dev,
  288.   byte *base, int sourcex, int sraster, gx_bitmap_id id,
  289.   int x, int y, int w, int h)
  290. {    check_rect();
  291.     return copy_byte_rect(mdev, base + x_to_byte(sourcex), sraster,
  292.         x_to_byte(x), y, x_to_byte(w), h);
  293. }
  294.  
  295. /* ------ 24- or 32-bit true color ------ */
  296.  
  297. /* Procedures */
  298. declare_mem_map_procs(mem_true_map_rgb_color, mem_true_map_color_rgb);
  299.  
  300. /* The device descriptor. */
  301. #define mem_true_procs(copy_mono, copy_color, fill_rectangle)\
  302.   mem_procs(mem_true_map_rgb_color, mem_true_map_color_rgb,\
  303.     copy_mono, copy_color, fill_rectangle)
  304.  
  305. /* We want the bytes of a color always to be in the order -,r,g,b, */
  306. /* but we want to manipulate colors as longs.  This requires careful */
  307. /* handling to be byte-order independent. */
  308. #define color_byte(cx,i) (((byte *)&(cx))[i])
  309.  
  310. /* Map a r-g-b color to a color index. */
  311. private gx_color_index
  312. mem_true_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  313.   gx_color_value b)
  314. {    gx_color_index color = 0;
  315.     color_byte(color, 1) = gx_color_value_to_byte(r);
  316.     color_byte(color, 2) = gx_color_value_to_byte(g);
  317.     color_byte(color, 3) = gx_color_value_to_byte(b);
  318.     return color;
  319. }
  320.  
  321. /* Map a color index to a r-g-b color. */
  322. private int
  323. mem_true_map_color_rgb(gx_device *dev, gx_color_index color,
  324.   gx_color_value prgb[3])
  325. {    prgb[0] = gx_color_value_from_byte(color_byte(color, 1));
  326.     prgb[1] = gx_color_value_from_byte(color_byte(color, 2));
  327.     prgb[2] = gx_color_value_from_byte(color_byte(color, 3));
  328.     return 0;
  329. }
  330.  
  331. /* ------ 24-bit color ------ */
  332. /* 24-bit takes less space than 32-bit, but is slower. */
  333.  
  334. /* Procedures */
  335. declare_mem_procs(mem_true24_copy_mono, mem_true24_copy_color, mem_true24_fill_rectangle);
  336.  
  337. /* The device descriptor. */
  338. private gx_device_procs mem_true24_procs =
  339.   mem_true_procs(mem_true24_copy_mono, mem_true24_copy_color,
  340.     mem_true24_fill_rectangle);
  341. gx_device_memory mem_true24_color_device =
  342.   mem_device("image(24)", 24, mem_true24_procs);
  343.  
  344. /* Convert x coordinate to byte offset in scan line. */
  345. #undef x_to_byte
  346. #define x_to_byte(x) ((x) * 3)
  347.  
  348. /* Unpack a color into its bytes. */
  349. #define declare_unpack_color(r, g, b, color)\
  350.     byte r = color_byte(color, 1);\
  351.     byte g = color_byte(color, 2);\
  352.     byte b = color_byte(color, 3)
  353. #define put3(ptr, r, g, b)\
  354.     ptr[0] = r, ptr[1] = g, ptr[2] = b
  355.  
  356. /* Fill a rectangle with a color. */
  357. private int
  358. mem_true24_fill_rectangle(gx_device *dev,
  359.   int x, int y, int w, int h, gx_color_index color)
  360. {    declare_unpack_color(r, g, b, color);
  361.     declare_scan_ptr(dest);
  362.     check_rect();
  363.     setup_rect(dest);
  364.     while ( h-- > 0 )
  365.        {    register int cnt = w;
  366.         register byte *pptr = dest;
  367.         do { put3(pptr, r, g, b); pptr += 3; } while ( --cnt > 0 );
  368.         inc_chunk_ptr(dest, draster);
  369.        }
  370.     return 0;
  371. }
  372.  
  373. /* Copy a monochrome bitmap. */
  374. private int
  375. mem_true24_copy_mono(gx_device *dev,
  376.   byte *base, int sourcex, int sraster, gx_bitmap_id id,
  377.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  378. {    byte *line;
  379.     int first_bit;
  380.     declare_unpack_color(r0, g0, b0, zero);
  381.     declare_unpack_color(r1, g1, b1, one);
  382.     declare_scan_ptr(dest);
  383.     check_rect();
  384.     setup_rect(dest);
  385.     line = base + (sourcex >> 3);
  386.     first_bit = 0x80 >> (sourcex & 7);
  387.     while ( h-- > 0 )
  388.        {    register byte *pptr = dest;
  389.         byte *sptr = line;
  390.         register int sbyte = *sptr++;
  391.         register int bit = first_bit;
  392.         int count = w;
  393.         do
  394.            {    if ( sbyte & bit )
  395.                {    if ( one != gx_no_color_index )
  396.                   put3(pptr, r1, g1, b1);
  397.                }
  398.             else
  399.                {    if ( zero != gx_no_color_index )
  400.                   put3(pptr, r0, g0, b0);
  401.                }
  402.             pptr += 3;
  403.             if ( (bit >>= 1) == 0 )
  404.                 bit = 0x80, sbyte = *sptr++;
  405.            }
  406.         while ( --count > 0 );
  407.         line += sraster;
  408.         inc_chunk_ptr(dest, draster);
  409.        }
  410.     return 0;
  411. }
  412.  
  413. /* Copy a color bitmap. */
  414. private int
  415. mem_true24_copy_color(gx_device *dev,
  416.   byte *base, int sourcex, int sraster, gx_bitmap_id id,
  417.   int x, int y, int w, int h)
  418. {    check_rect();
  419.     return copy_byte_rect(mdev, base + x_to_byte(sourcex), sraster,
  420.         x_to_byte(x), y, x_to_byte(w), h);
  421. }
  422.  
  423. /* ------ 32-bit color ------ */
  424.  
  425. /* Procedures */
  426. declare_mem_procs(mem_true32_copy_mono, mem_true32_copy_color, mem_true32_fill_rectangle);
  427.  
  428. /* The device descriptor. */
  429. private gx_device_procs mem_true32_procs =
  430.   mem_true_procs(mem_true32_copy_mono, mem_true32_copy_color,
  431.     mem_true32_fill_rectangle);
  432. gx_device_memory mem_true32_color_device =
  433.   mem_device("image(32)", 32, mem_true32_procs);
  434.  
  435. /* Convert x coordinate to byte offset in scan line. */
  436. #undef x_to_byte
  437. #define x_to_byte(x) ((x) << 2)
  438.  
  439. /* Fill a rectangle with a color. */
  440. private int
  441. mem_true32_fill_rectangle(gx_device *dev,
  442.   int x, int y, int w, int h, gx_color_index color)
  443. {    declare_scan_ptr(dest);
  444.     check_rect();
  445.     setup_rect(dest);
  446.     while ( h-- > 0 )
  447.        {    gx_color_index *pptr = (gx_color_index *)dest;
  448.         int cnt = w;
  449.         do { *pptr++ = color; } while ( --cnt > 0 );
  450.         inc_chunk_ptr(dest, draster);
  451.        }
  452.     return 0;
  453. }
  454.  
  455. /* Copy a monochrome bitmap. */
  456. private int
  457. mem_true32_copy_mono(gx_device *dev,
  458.   byte *base, int sourcex, int sraster, gx_bitmap_id id,
  459.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  460. {    byte *line;
  461.     int first_bit;
  462.     declare_scan_ptr(dest);
  463.     check_rect();
  464.     setup_rect(dest);
  465.     line = base + (sourcex >> 3);
  466.     first_bit = 0x80 >> (sourcex & 7);
  467.     while ( h-- > 0 )
  468.        {    register gx_color_index *pptr = (gx_color_index *)dest;
  469.         byte *sptr = line;
  470.         register int sbyte = *sptr++;
  471.         register int bit = first_bit;
  472.         int count = w;
  473.         do
  474.            {    if ( sbyte & bit )
  475.                {    if ( one != gx_no_color_index )
  476.                   *pptr = one;
  477.                }
  478.             else
  479.                {    if ( zero != gx_no_color_index )
  480.                   *pptr = zero;
  481.                }
  482.             if ( (bit >>= 1) == 0 )
  483.                 bit = 0x80, sbyte = *sptr++;
  484.             pptr++;
  485.            }
  486.         while ( --count > 0 );
  487.         line += sraster;
  488.         inc_chunk_ptr(dest, draster);
  489.        }
  490.     return 0;
  491. }
  492.  
  493. /* Copy a color bitmap. */
  494. private int
  495. mem_true32_copy_color(gx_device *dev,
  496.   byte *base, int sourcex, int sraster, gx_bitmap_id id,
  497.   int x, int y, int w, int h)
  498. {    check_rect();
  499.     return copy_byte_rect(mdev, base + x_to_byte(sourcex), sraster,
  500.         x_to_byte(x), y, x_to_byte(w), h);
  501. }
  502.